package com.hnly.djdl.home.p3

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hnly.djdl.model.Snack
import com.hnly.djdl.model.listTopics
import com.hnly.djdl.model.topBanners
import com.hnly.djdl.components.MainDivider
import com.hnly.djdl.components.MainBoxSurface
import com.hnly.djdl.components.SnackImage
import com.hnly.djdl.components.TitleText
import com.hnly.djdl.components.banner.Banner
import com.hnly.djdl.theme.MainMargin
import com.hnly.djdl.theme.MainTheme
import com.hnly.djdl.theme.MainTitleColor

@Composable
fun P3Page(onSnackClick: (Snack) -> Unit, modifier: Modifier = Modifier) {
    val bannerLines = topBanners
    val orderLines = listTopics
    MainBoxSurface(modifier = modifier.fillMaxSize()) {
        Box {
            LazyColumn(
                modifier = modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                item { TitleText(text = "A面A面知识", modifier = Modifier) }
                item {
                    Banner(
                        data = bannerLines,
                        onImagePath = { pos ->
                            bannerLines[pos].imageUrl
                        },
                        pagerIndicatorModifier = Modifier
                            .background(Color(0x90000000))
                            .padding(horizontal = 10.dp)
                            .padding(top = 10.dp, bottom = 10.dp), //指示器Row的整个样式
                        onBannerItemClick = { pos ->
                            onSnackClick(bannerLines[pos])
                        }
                    )
                }

                items(orderLines) { orderLine ->
                    CartItem(
                        snack = orderLine,
                        onSnackClick = onSnackClick
                    )
                }
            }
        }
    }
}


@Composable
fun CartItem(
    snack: Snack,
    onSnackClick: (Snack) -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier
        .fillMaxWidth()
        .clickable { onSnackClick(snack) }
        .padding(horizontal = MainMargin)) {
        Text(
            text = snack.name,
            style = TextStyle.Default.copy(
                fontSize = 24.sp,
                fontWeight = FontWeight.Bold,
            ),
            color = MainTitleColor.copy(alpha = 0.9f),
            modifier = Modifier.padding(vertical = 6.dp)
        )
        Row(modifier = Modifier.fillMaxWidth()) {
            SnackImage(
                imageUrl = snack.imageUrl,
                contentDescription = null,
                modifier = Modifier.size(100.dp)
            )
            Spacer(modifier = Modifier.width(8.dp))
            Text(
                text = snack.tagline,
                style = MaterialTheme.typography.body2,
                color = MainTheme.colors.textHelp,
                maxLines = 6,
                overflow = TextOverflow.Ellipsis,
                modifier = Modifier
            )
        }
        MainDivider(Modifier.padding(vertical = 8.dp))
    }
}

@Preview("default")
@Composable
private fun P3PagePreview() {
    MainTheme {
        P3Page(
            onSnackClick = {}
        )
    }
}
